package beta;

import javax.microedition.lcdui.Graphics;


public class SwitchListItem extends Item
{
    private String[] items;
    private LabelItem label;
    private int labelHeight;
    private int selectedItem = 0;
    
    public SwitchListItem(String l, String[] i)
    {
        super(true);
        items = i;
        label = new LabelItem(l);
    }

    boolean onKeyPressed(int key)
    {
        switch(key)
        {
            case Screen.LEFT_KEY:
                selectedItem --;
                if(selectedItem < 0)
                    selectedItem = items.length - 1;
                repaint();
                break;
            case Screen.RIGHT_KEY:
                selectedItem ++;
                if(selectedItem >= items.length)
                    selectedItem = 0;
                repaint();
                break;
        }
        return false;
    }

    public int getPreferredHeight(int width)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        labelHeight = label.getPreferredHeight(width);
        return theme.defaultFont.getHeight() + labelHeight;
    }

    public void print(Graphics g, boolean selected, int width, int height)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        if(selected)
            theme.drawSelectionBorder(g, width, height);
        label.print(g, false, width, labelHeight);
        g.setFont(theme.defaultFont);
        g.drawString(items[selectedItem], 2, labelHeight - 1, 0);
    }

}
